class Solution {
public:
    vector<int> findAnagrams(string s, string p) {
        int len_s = s.size(), len_p = p.size();
        if (len_p > len_s)
            return {};

        vector<int> ret;
        int p_hash[27] = {0};   //记录p中每个字母出现的次数
        for (auto e : p)    
            p_hash[e - 'a']++;

        int left = 0, right = 0;
        int s_hash[27] = {0};
        for (; right < len_s; right++)
        {
            s_hash[s[right] - 'a']++;

            while (s_hash[s[right] - 'a'] > p_hash[s[right] - 'a'])
                s_hash[s[left++] - 'a']--;

            //如果窗口大小和目标字符串长度相等
            //并且窗口内出现的字符个数和目标字符串内的字符的个数（每个字符各自的个数）一致
            //就说明窗口内的字符串就是一个字母异位词    
            if (right - left + 1 == len_p)
                ret.push_back(left);
        }
        return ret;
    }
};